package com.point.business.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.JsonObject;
import com.point.business.database.managers.UsersManager;
import com.point.business.database.model.User;
import com.point.business.util.UTFUtil;
import com.point.business.util.Role;

/**
 * Servlet implementation class LoginServlet
 */

public class NewUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public NewUserServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		UTFUtil.utf8(request, response);
		PrintWriter out = response.getWriter();
		User newUser = new User();
		String fullName = request.getParameter("fullName");
		String password = request.getParameter("password");
		String email = request.getParameter("email");
		String username = request.getParameter("username");
		int maxQueries = Integer.parseInt(request
				.getParameter("maxSearchQueries"));
		newUser.setFullName(fullName);
		newUser.setPassword(password);
		newUser.setEmail(email);
		newUser.setUsername(username);
		newUser.setRole(Role.USER);
		boolean canDoSL = false;
		if (request.getParameter("canDoSL") != null) {
			canDoSL = true;
			newUser.setCanDoSL(true);
		} else {
			newUser.setCanDoSL(false);
		}
		boolean canDoUOPB = false;
		if (request.getParameter("canDoUOPB") != null) {
			canDoUOPB = true;
			newUser.setCanDoUOPB(true);
		} else {
			newUser.setCanDoUOPB(false);
		}
		boolean canDoND = false;
		if (request.getParameter("canDoNO") != null) {
			canDoND = true;
			newUser.setCanDoNO(true);
		} else {
			newUser.setCanDoNO(false);
		}
		boolean canDoNO = false;
		if (request.getParameter("canDoND") != null) {
			canDoNO = true;
			newUser.setCanDoND(true);
		} else {
			newUser.setCanDoND(false);
		}
		boolean isCreated;
		JsonObject jsono = new JsonObject();
		try {
			// FIXME real values for:
			// company,address,city,telephone,cellPhone
			isCreated = UsersManager.getInstance().createNewUser(email,
					fullName, password, Role.USER, "", username, "", canDoNO,
					canDoND, canDoSL, canDoUOPB,maxQueries, "", "", "", "", "");
		} catch (Exception e) {
			jsono.addProperty("errorMsg",
					"Корисничкото име веќе постои во базата, одберете друго");
			out.print(jsono);
			return;
		}
		if (isCreated) {
			jsono.addProperty("success", true);
			jsono.addProperty("newUser", true);
			out.print(jsono);
		} else {
			jsono.addProperty("errorMsg", "Some errors occured.");
			out.print(jsono);
		}
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}
}
